package com.gjintel.framework.common.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.framework.res.model.Resources;
import com.gjintel.framework.res.service.IResService;
import com.gjintel.sdk.util.FileDownload;

@RequestMapping("/office/plugin")
@Controller
public class OfficePluginController {

	@Resource
	private IResService resService;
	
	//转到文件下载
	@RequestMapping("down")
	public void fileView(String resId, HttpServletResponse response, HttpServletRequest request) {
		Resources resource = this.resService.get(Resources.class, resId);
		OutputStream os = null;
		try {
			if (resource == null) {
				PrintWriter out = response.getWriter();
				response.setHeader("Content-Disposition", "attachment;");
				response.addHeader("Pargam", "no-cache");
				response.addHeader("Cache-Control", "no-cache");
				out.write("找不到对应的文件数据！");
				out.flush();
			} else {
				String fileRealPath = resource.getFilePath() + File.separator + resource.getId() + resource.getExeName();
				String filePath = request.getSession().getServletContext().getRealPath("/");
				fileRealPath = filePath + "file_resource" + fileRealPath;
				String fileName = resource.getOldName();
				os = response.getOutputStream();
				
				//下载文件
				FileDownload.processDownload(fileRealPath, fileName, request, response);
			}
		} catch (Exception e) {
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//打开详细插件
	@RequestMapping("detail")
	public ModelAndView openDetail(String resId, HttpServletResponse response, HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("word_plugin/open");
		Resources resource = this.resService.get(Resources.class, resId);
		String fileRealPath="";
		String prefix="";
		if (resource != null) {
			//文件路径
			fileRealPath = resource.getFilePath() + File.separator + resource.getId() + resource.getExeName();
			fileRealPath ="file_resource" + fileRealPath;
			prefix=resource.getExeName().toLowerCase();
			
			//判断扩展名
			String[] preFixs={".doc",".docx",".xls","xlsx",".jpg",".jpeg",".png",".gif",".pdf"};
			List<String> preFixList=Arrays.asList(preFixs);
			
			//不能打开的文件类型
			if(!preFixList.contains(prefix)){
				mv.setViewName("error/file-error");
			}else{
				List<String> preFixWord=Arrays.asList(new String[]{".doc",".docx",".xls",".xlsx",".pdf"});
				List<String> preFixImg=Arrays.asList(new String[]{".jpg",".jpeg",".png",".gif"});
				if(preFixWord.contains(prefix.toLowerCase())){
					mv.setViewName("word_plugin/open");
				}
				if(preFixImg.contains(prefix)){
					mv.setViewName("word_plugin/open-img");
				}
			}
			
		}
		mv.addObject("filepath",fileRealPath);
		mv.addObject("prefix",prefix);
		return mv;
	}
	
	
	//转到打开文件
	@RequestMapping("open")
	public ModelAndView openFile(String resId,String urlStr, HttpServletResponse response, HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("word_plugin/show-frame");
		Resources resource = this.resService.get(Resources.class, resId);
		
		mv.addObject("resId", resId);
	//	mv.addObject("urlStr", URLDecoder.decode(urlStr,"utf-8"));
		
		//通过有resouce反查得到project Id;
		String projId= resource.getBusinessId().split(",")[0];
		mv.addObject("projId", projId);
		
		String fileRealPath="";
		String prefix="";
		if (resource != null) {
			//文件路径
			fileRealPath = resource.getFilePath() + File.separator + resource.getId() + resource.getExeName();
			fileRealPath ="file_resource" + fileRealPath;
			prefix=resource.getExeName();
			
			//判断扩展名
			String[] preFixs={".doc",".docx",".xls","xlsx",".jpg",".jpeg",".png",".gif",".pdf"};
			List<String> preFixList=Arrays.asList(preFixs);
			
			//不能打开的文件类型
			if(!preFixList.contains(prefix.toLowerCase())){
				mv.setViewName("error/file-error");
			}
		}
		mv.addObject("filepath",fileRealPath);
		mv.addObject("prefix",prefix);
		return mv;
	}
	
	//转到打开图片
	@RequestMapping("openImg")
	public ModelAndView openImg(String resId, HttpServletResponse response, HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("word_plugin/open-img");
		Resources resource = this.resService.get(Resources.class, resId);
		String fileRealPath="";
		String prefix="";
		if (resource != null) {
			//文件路径
			fileRealPath = resource.getFilePath() + File.separator + resource.getId() + resource.getExeName();
			fileRealPath ="file_resource" + fileRealPath;
			prefix=resource.getExeName();
			
			//判断扩展名
			String[] preFixs={".doc",".docx",".xls","xlsx",".jpg",".jpeg",".png",".gif",".pdf"};
			List<String> preFixList=Arrays.asList(preFixs);
			
			//不能打开的文件类型
			if(!preFixList.contains(prefix.toLowerCase())){
				mv.setViewName("error/file-error");
			}
		}
		mv.addObject("filepath",fileRealPath);
		mv.addObject("prefix",prefix);
		return mv;
	}
}
